package org.example.thymeleaf.demo.demos.controller;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import com.alibaba.fastjson2.JSONObject;
import org.example.thymeleaf.demo.demos.message.Result;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author wzx
 * @date 2024/9/20 15:17
 * @description: 非对称加密RSA测试
 */
@RestController
@RequestMapping("/symmetric")
public class SymmetricCryptoController {


    // 私钥解密
    private static final String PRIVATE_KEY_BASE64 = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC+jiRsbYqzu1co\n" +
            "gChz64ogDtvHcqjaRFZtMSHcqlFUzrUqqRNcCD0b3tl4FtsTpFn6WnF15+gb0cTc\n" +
            "gK59nxvWJeAGvfX9bNzk/uwy6c//WaDI26B/jYHlRbFhsiK85EvERd7C/ahfDuuK\n" +
            "3Ikrsyo5SVKIXUOr20T/IMspQO6zt6mZl/ocw23wLgKR6kzn22IhK6vxOD0r4gDZ\n" +
            "HmC6EjM9Y4DA4anhFm/fpCN7UVW2ZaBvOF8sQSZL7f3v4RI0h1lybqQKj7AO57Gp\n" +
            "mLSYplPJkubfjQCxFJR+HWuMI9QdXD6gBL7sR3BKD8QHtTo+CIybSA3j/nEDmEjh\n" +
            "jLbafPwPAgMBAAECggEAK9o2U6WZ5+bTFgT4SkUIUnxY47tlVGWpphNKgafIc8Xm\n" +
            "N10VkXZr6cANN++6lviZpF2wArxN1CkgLsaGYW5+T3gD6ElLFPk6lJB99vGL5m3P\n" +
            "7KludCwymnrBaYQ/Lk0zioWx0Bnj54xddGVwQ9hFfU18atlbu28In9YCKwpAZKcX\n" +
            "AyJ/+2TyzvVt9JcG48HYMdki3AJFtqK18uy5klwTamSM/QMWXfybzqIJ9Qo4QCqC\n" +
            "VMnkqeXggi7PygHvnXfzlYeaB9WyLoXkDxTVdg0CGK8OpyYHF+aHN8IjJElhJayO\n" +
            "89Hp8I0Nv9VICqyVfBH0u63otMdtJ/NQFu7RF2TLYQKBgQD9xBTqlp4uoQte7mS8\n" +
            "QvJth7oNqxhcoMmsJUebl98Exs+V7Ie7m+oaU3IYUf8UMAhOoNtCp4wFbPpvTuaI\n" +
            "R2Z8NHLgkk7J4gU2/uYrV6GwUckHMNrRYVODSAY12AJPnt4wJ2ooZN1sZBX6N7aZ\n" +
            "SdHU33BWJ6oXzScGNEChjVKM6QKBgQDAO5njdiycSyjNXDb8HAUqt6J9stcHq3Cn\n" +
            "KvM+aRa8skIufqp3IRqmoLPZi6GJN0mNs61OJ07e8X+Xk75jx3QndfFzbjWpgclq\n" +
            "dQt4VT42IKAHBjxZGjmYwoJvebtg3XMDS76CQRdqTbyMeDqlScWFs3JX2wQY27nf\n" +
            "v7/YOdFGNwKBgQCWTKyWxN6rf3bcoxXG61/qv5jSv6OOOS05aFshLLJKgfYxNgau\n" +
            "d4oQTZwmC2qo6koBSASVVeb4z7MEehbQYHmy1t2zZPSFXfj4ucBZq214oPgP6j+w\n" +
            "A5UTDHWhLm8v+Uhf/PhDNHpw2Far/JOJ9s7w5eYDITr9HeUfJ5OgG7PpAQKBgFqy\n" +
            "hDvuwMOJLpod3Q98BPAPrknSPGQw/jjRKe0TVhf3nwQi3TjR1Kozfkew+qkr/bcu\n" +
            "xCDwqOTFWqbgnV6kd2g63I3SvNzaEHb6/EttJKoTII3P4l9D4VLyyunfrInaUe6H\n" +
            "KT55iZutS72W6EyeDuSzMsrC2h8EZKFt7g0N0nBnAoGAdJ+QVPsQW/o0xGannhSC\n" +
            "cVnUYYedFQ3JCo7YPf3kuy7w4+EUASXfq+fj7N2HqJxeBlThCJA6RBOW8XCLIdD+\n" +
            "6Lh4Uk02cKnu4xSfSrl2vtmbdi0rUtft82xisx7+7W95dmPwe6CN77IDB2XXj8hs\n" +
            "5cjcMM01jxkwrFBm3tZZGHM=";

    // 公钥加密
    private static final String PUBLIC_KEY_BASE64 = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvo4kbG2Ks7tXKIAoc+uK\n" +
            "IA7bx3Ko2kRWbTEh3KpRVM61KqkTXAg9G97ZeBbbE6RZ+lpxdefoG9HE3ICufZ8b\n" +
            "1iXgBr31/Wzc5P7sMunP/1mgyNugf42B5UWxYbIivORLxEXewv2oXw7rityJK7Mq\n" +
            "OUlSiF1Dq9tE/yDLKUDus7epmZf6HMNt8C4CkepM59tiISur8Tg9K+IA2R5guhIz\n" +
            "PWOAwOGp4RZv36Qje1FVtmWgbzhfLEEmS+397+ESNIdZcm6kCo+wDuexqZi0mKZT\n" +
            "yZLm340AsRSUfh1rjCPUHVw+oAS+7EdwSg/EB7U6PgiMm0gN4/5xA5hI4Yy22nz8\n" +
            "DwIDAQAB";


    @GetMapping("/rsa")
    public Result<?> encryptRsa(){
        Map<String,String> map = new HashMap<>();
        map.put("name","wzx");
        map.put("age","18");
        map.put("sex","男");
        //加密为16进制表示
        String jsonString = JSONObject.toJSONString(map);
        RSA rsa = new RSA(PRIVATE_KEY_BASE64, PUBLIC_KEY_BASE64);
        // 私钥加密
//      String encryptHex = rsa.encryptHex(jsonString, CharsetUtil.CHARSET_UTF_8, KeyType.PrivateKey);
        // 公钥加密
        String encryptHex = rsa.encryptBase64(jsonString, CharsetUtil.CHARSET_UTF_8, KeyType.PublicKey);
        return Result.success(encryptHex);
    }

    @GetMapping("/decodeRsa")
    public Result<?> decodeRsa(@RequestParam String encryptHex){
        //解密为字符串
        RSA rsa = new RSA(PRIVATE_KEY_BASE64, PUBLIC_KEY_BASE64);
        // 公钥解密
        // String decryptStr = rsa.decryptStr(encryptHex, KeyType.PublicKey, CharsetUtil.CHARSET_UTF_8);
        // 私钥解密
        String decryptStr = rsa.decryptStr(encryptHex, KeyType.PrivateKey, CharsetUtil.CHARSET_UTF_8);
        return Result.success(JSONObject.parseObject(decryptStr, Map.class));
    }


}
